package cn.tedu.supermarket.controller;

import cn.tedu.supermarket.entity.User;
import cn.tedu.supermarket.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private UserMapper userMapper;
    @RequestMapping(value = "/checklogin", produces = "text/html;charset=utf-8")
    public String checkLogin(String username, String password, ModelMap map, HttpSession session) {
        System.out.println("checklogin开始");
        System.out.println(username);
        if (username==""||password==""){
            map.put("a","账号或密码错误");
            return "redirect:loginNow";
        }else {
            try {
                String pwd = userMapper.selectBuyUserPassword(username);
                if(password.equals(pwd)){
                    System.out.println("登陆成功");
                    User user = userMapper.selectBuserByNamePwd(username,password);
                    System.err.println(user);
                    session.setAttribute("user",user);
                    return "redirect:bIndex";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            map.put("a","账号或密码错误");
            System.out.println("账号或密码错误");
            return "redirect:loginNow";
        }
    }

    @RequestMapping(value = "/suserLogin", produces = "text/html;charset=utf-8")
    public String suserLogin(String username, String password, ModelMap map,HttpSession session) {
        System.out.println("checklogin开始");
        System.out.println(username);
        if (username==""||password==""){
            map.put("a","账号或密码错误");
            return "redirect:sLoginNow";
        }else {
            try {
                String pwd = userMapper.selectSelUserPassword(username);
                if(password.equals(pwd)){
                    System.out.println("登陆成功");
                    User user = userMapper.selectSuserByNamePwd(username,password);
                    session.setAttribute("user",user);
                    return "redirect:sIndex";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            map.put("a","账号或密码错误");
            System.out.println("账号或密码错误");
            return "redirect:sLoginNow";
        }
    }
}
